<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pembayaran_bulanan extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->isLogin();
		$this->num_post=10;		
		$this->load->model("master/master_set_bulanan"); 
		$this->load->model("master/master_tapel"); 	
		$this->load->model("master/master_siswa"); 
		$this->load->model("master/master_komponen"); 
		$this->load->model("pembayaran_bulanan_model"); 
	}

	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}
	
	function index(){
		$tapel=$this->master_tapel->get_tapel_aktif();
		$data['list_komponen']=array();
		$list_komponen=$this->master_komponen->popup("bulanan");
		foreach($list_komponen as $val){
			$data['list_komponen'][$val['id_komp']]=$val['nama_komp'];
		}
		$data['main_view']='pembayaran_bulanan/view';
		$this->load->view('index',$data);
	}
	
	function view_pembayaran(){
		$data['main_view']='pembayaran_bulanan/view_pembayaran';
		$nis=$this->input->post('nis');
		$komponen=$this->input->post('komponen');
		if(empty($nis)){
			$nis=$this->uri->segment(3);
		}else{
			$nis=$this->input->post('nis');
		}
		if(empty($komponen)){
			$komponen=$this->uri->segment(4);
		}else{
			$komponen=$this->input->post('komponen');
		}
		//echo 'nisku '.$nis.' komponen '.$komponen;exit;
		
		//cek nis 
		$cek_nis=$this->master_siswa->cek_nis($nis);
		$data['detail_siswa']=$this->master_siswa->detail($nis);
		if($cek_nis==false){
			$this->session->set_flashdata('pesan_error', 'NVA tidak ditemukan');
			redirect('pembayaran_bulanan');
		}
		//cek komponen
		$cek_komponen=$this->master_set_bulanan->cek_komponen($data['detail_siswa'][0]->id_tapel_masuk,$komponen);
		if($cek_komponen==false){
			$this->session->set_flashdata('pesan_error', 'Komponen Pembayaran Bulanan belum di Set');
			redirect('pembayaran_bulanan');
		}
		//echo 'tapel siswa '.$data['detail_siswa'][0]->id_tapel_masuk;
		//print_r($data['detail_siswa']);
		$tapel_aktif=$this->master_tapel->get_tapel_aktif();
		$periode=$this->master_tapel->get_periode_aktif();
		//cek tagihan pembayaran
		$cek_tagihan=$this->pembayaran_bulanan_model->cek_tagihan($nis,$komponen,$tapel_aktif['id_mr_th_ajaran']);
		
		if($cek_tagihan==true){
			//ada tagihan, tampilkan tagihan yg belum bayar, dan nanti akan mengupdate tagihan
			$data['status_aksi']="update";
			$list_tagihan=$this->pembayaran_bulanan_model->get_tagihan_belum_bayar($nis,$komponen,$tapel_aktif['id_mr_th_ajaran']);
			foreach($list_tagihan as $key=>$val){
				$list_tagihan[$key]['str_periode']=konvert_periode($val['periode']);
				$list_tagihan[$key]['nominal']=$val['nominal'];
			}
		}else{
			//tidak ada tagihan, berarti insert tagihan
			$list_tagihan=$this->pembayaran_bulanan_model->get_tagihan($nis,$komponen,$tapel_aktif['id_mr_th_ajaran']);
			$id_lembaga=$this->session->userdata('id_lembaga');
			$nominal_bayar=$this->master_set_bulanan->get_nominal_komponen($komponen,$data['detail_siswa'][0]->id_tapel_masuk,$id_lembaga);
			foreach($list_tagihan as $key=>$val){
				$list_tagihan[$key]['str_periode']=konvert_periode($val['periode']);
				$list_tagihan[$key]['nominal']=$nominal_bayar;
			}
			//echo "<pre>";
			//print_r($list_tagihan);
			$data['status_aksi']="insert";
			//exit;
			
		}
		
		$list_lunas=$this->pembayaran_bulanan_model->get_tagihan_lunas($nis,$komponen,$tapel_aktif['id_mr_th_ajaran']);
		
		foreach($list_lunas as $key=>$val){
			$list_lunas[$key]['str_periode']=konvert_periode($val['periode']);
		}
		
		/*
		$data['hitung_denda']=$this->hitung_denda($nominal_bayar,count($list_tagihan));
		if($data['hitung_denda']['denda']>0){
			//ada denda
			$data['status_denda']='';
		}else{
			$data['status_denda']='none';
		}
		*/
		//echo "<pre>";
		//print_r($hitung_denda);
		$data['list_lunas']=$list_lunas;
		$data['det_komponen']=$this->master_komponen->edit($komponen);
		$data['list_tagihan']=$list_tagihan;
		$data['tapel']=$tapel_aktif;
		$data['nis']=$nis;
		$data['komponen']=$komponen;
		$data['periode']=konvert_periode($periode['periode']);
		//print_r($list_tagihan);exit;
		$this->load->view('index',$data);
	}
	
	
	
	function proses_pembayaran(){
		$nis=$this->input->post('nis');
		$periode=$this->input->post('id_periode');
		$komponen=$this->input->post('komponen');
		$nominal=$this->input->post('nominal');
		
		$jml=count($periode);
		$status_aksi=$this->input->post('status_aksi');
		if($status_aksi=="insert"){
			//insert tagihan
			for($i=0;$i<$jml;$i++){
				$angka=str_replace(".","",$nominal[$i]);
				$angka=str_replace(",",".",$angka);
				//echo $angka.' ';
				$insert=$this->pembayaran_bulanan_model->insert_tagihan($periode[$i],$komponen,$angka,$nis);
				
			}
		}else{
			for($i=0;$i<$jml;$i++){
				$angka=str_replace(".","",$nominal[$i]);
				$angka=str_replace(",",".",$angka);
				//echo $angka.' ';
				$insert=$this->pembayaran_bulanan_model->update_tagihan($periode[$i],$angka);
				
			}
		}
		//exit;
		
		$this->session->set_flashdata('pesan', 'Setting Tagihan Sukses Diinput');
		redirect('pembayaran_bulanan');
	}
	
	function delete_pembayaran($id,$nis,$komponen){
		$delete=$this->pembayaran_bulanan_model->delete($id);
		$this->session->set_flashdata('pesan', 'Pembayaran berhasil dihapus');
		redirect('pembayaran_bulanan/view_pembayaran/'.$nis.'/'.$komponen);
	}
	
	
	
	
}

?>